<template>
  <div>
    <el-dialog
      :title="showTitle"
      :visible="showDialog"
      @close="btnCancel"
    >
      <el-form ref="formRef" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @focus="getEmployeeSimple">
            <el-option v-for="item in peoples" :key="item.id" :value="item.username" :label="item.username" />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取 消</el-button>
          <el-button size="small" type="primary" @click="btnOk">确 定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { addDepartments, getDepartDetail, getDepartments, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'

export default {
  name: 'AddDept',
  props: {
    showDialog: Boolean,
    treeNode: Object
  },
  data() {
    const checkName = async(rules, value, callback) => {
      console.log(this.treeNode)
      // 1、 接口获取所有的部门
      const { depts } = await getDepartments()
      // 2、找到当前点击的部门的所有子部门
      let currentNodeChilren
      if (this.formData.id) {
        currentNodeChilren = depts.filter(item => item.pid === this.treeNode.id && item.id !== this.treeNode.id)
      } else {
        currentNodeChilren = depts.filter(item => item.pid === this.treeNode.id)
      }
      // 3、判断当前部门的所有子部门名称是否包含用户输入的部门名称
      const isRepeat = currentNodeChilren.some(item => item.name === value)
      console.log(depts)
      // 4、如果包含说明名称重复
      if (isRepeat) {
        callback(new Error('部门重复了'))
      } else {
        callback()
      }
    }
    const checkCode = async(rules, value, callback) => {
      const { depts } = await getDepartments()
      let isRepeat
      if (this.formData.id) {
        isRepeat = depts.some(item => item.code === value && item.id !== this.treeNode.id)
      } else {
        isRepeat = depts.some(item => item.code === value)
      }
      if (isRepeat) {
        callback(new Error('编码重复了'))
      } else {
        callback()
      }
    }
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          {
            required: true,
            message: '部门名称不能为空',
            trigger: 'blur'
          },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          {
            validator: checkName,
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '部门编码不能为空',
            trigger: 'blur'
          },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          {
            validator: checkCode,
            trigger: 'blur'
          }
        ],
        manager: [
          {
            required: true,
            message: '部门负责人不能为空',
            trigger: 'blur'
          }
        ],
        introduce: [
          {
            required: true,
            message: '部门介绍不能为空',
            trigger: 'blur'
          },
          { min: 1, max: 300, message: '部门介绍要求1-50个字符', trigger: 'blur' }
        ]
      },
      peoples: []
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增子部门'
    }
  },
  methods: {
    async getDetails(id) {
      this.formData = await getDepartDetail(id)
    },
    async getEmployeeSimple() {
      this.peoples = await getEmployeeSimple()
    },
    async btnOk() {
      try {
        await this.$refs.formRef.validate()
        if (this.formData.id) {
          await addDepartments({
            ...this.formData,
            pid: this.treeNode.id
          })
        } else {
          await updateDepartments(this.formData)
        }

        this.$message.success(this.formData.id ? '更新成功' : '添加成功')
        this.$emit('getDepartments')
        this.btnCancel()
      } catch (e) {
        console.log(e)
      }
    },
    btnCancel() {
      // 重置数据  因为resetFields 只能重置 表单上的数据 非表单上的 比如 编辑中id 不能重置
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.formRef.resetFields()
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style scoped>

</style>
